Tutoria: Basic Tools wey you need for Private Deep Learning

Make you na welicome to Pysyft's introductory tutoria wey dey preserve privacy and also dey make deep learning dey accessible for everybodi. This series of notebooks na the tori wey go make you know the new tools and techniques wey you need to get before you go fit dey do deep learning on secret/private data/models so that everybodi go fit get am.

Scope: Make you know sey we no go onli talk about how to make everbodi get am/ protect the data from tiff, but we go talk about how Pysyft fit helep everybodi get control of their data, we go talk about Databases where we keep data and how we fit go ask for am, we got talk about neural models wey we fit use to commot information from data. We no sey nobodi dey one place, so as we dey add more tins to Pysyft, we go dey put am here and this tutorial go dey explain how we go dey use am. All of us go come dey happy.

Person wey write am:

Person wey translate am:

Outline:

  • Part 1: Basic Tools wey you need for Private Deep Learning

You know why this tutoria dey important?

1) Competitive Career Advantage - For 20 years wey digital revolution don dey start, tins don dey change oh. Tins like: data don dey berekete because computer don helep us dey reduce the things wey humanbeings dey do by dem sef and as computer dey helep us he come dey commot data. As data come dey berekete, GDPR new regulation dey control how companies fit use the data - and more importantly how dey fit use personal information. You no sey my matter na my matter, make everybodi mind their business. Bottom Line: Scientist wey dey study data for school no get the right tools to access data because dey still dey use "old school" tools, but if you come learn Private Deep Learning tools, YOU go dey lead you mate and you go come get competitive advantage for your career.

2) Entreprenuerial Opportunities - Wahala wey Deep Learning fit solve for our society dey too plenti, but this important problem neva get solution because we go need access to incredibly sensitive information about the people wey wan use the solution(imagine sey I wan use Deep Learning to helep person wey get mental and relationship palava!). He go hard make dem give you the data. You know sey na key dey open any door so if you learn Private Deep Learning, you don get the key to unlock startup opportunities wey person wey no get the key no go fit open.

3)Social Good - In this life wey wahala dey so, Deep Learning fit solve them for us. Deep Learning on personal information na Deep Learning about people, for people. To dey learn how to do Deep Learning on top data wey no be your own na serious career and entrepreneurial opportunity, this one go give person opportunity to helep solve some of the most personal and important problems wey dey people life. You go fit solve this wahala on a large scale wey he touch plenti life.

If you do the tins wey dey below, we go get extra credit.

Oya make we carry go !!!

Part -1: Prerequisites

  • Know PyTorch wella - if you no know am use this link to learn http://fast.ai course, if you don finish the course come back.
  • Read the PySyft Framework Paper https://arxiv.org/pdf/1811.04017.pdf! If you read am wella, you go don get the background knowledge on how we build Pysyft and you go come understand am wella.

Part 0: Setup

Before you start anytin, check if everytin wey you need don dey installed. To do this, carry go Pysyft's readme and follow all the tins wey you suppose do. TLDR for plent people na.

  • Install Python 3.6 or higher
  • Install PyTorch 1.4
  • pip install syft[udacity]

If you follow all the tins but he no come work(or any of the steps come fail) - first check the [README] (https://github.com/OpenMined/PySyft.git) for installation help and then make you open GitHub Issue or ping #beginner channel wey dey our slack! slack.openmined.org. We go helep you because we full ground berekete.


In [ ]:
# Run this cell to see if things work
import sys

import torch
from torch.nn import Parameter
import torch.nn as nn
import torch.nn.functional as F

import syft as sy
hook = sy.TorchHook(torch)

torch.tensor([1,2,3,4,5])

If this cells work, you don start the race be that! Oya make we carry go!

Part 1: Basic Tools wey need for Private, Decentralized Data Science

You know sey Deep Learning need data. I know sey you go come dey think sey how you go train a model when you get the data wey you go use?

The answer simple die. If you dey use Pytorch, I know sey you go sabi how dem dey use torch.tensor objects like the cell wey dey below.


In [ ]:
x = torch.tensor([1,2,3,4,5])
y = x + x
print(y)

To dey use these super fancy (and powerful!) tenseors dey important, but data must to dey your local machine. Na here we go start work.

Section 1.1 - Make we send Tensors go Bob's Machine

Normally na to dey do data science/ deep learning on top machine wey get data, as we dey so we want perform this kind computation on some other machine. He come dey clear to us now sey, we no fit assume sey the data dey our machine.

As we no come sure, instead make we go use Torch tensors, now we go come work with pointers to tensors. Make I show you wetin I dey talk. First of all, we go create a "pretend" machine wey "pretend" person get - we go call am Bob.


In [ ]:
bob = sy.VirtualWorker(hook, id="bob")

Make we say Bob machine dey another planet - make we say Mars! This time wey we dey talk, the machine dey empty. Oya make we create data wey we go send to Bob and make we come learn about pointers!


In [ ]:
x = torch.tensor([1,2,3,4,5])
y = torch.tensor([1,1,1,1,1])

Na now we go send tensors to Bob!!


In [ ]:
x_ptr = x.send(bob)
y_ptr = y.send(bob)

In [ ]:
x_ptr

BOOM! Bob don get two tensors! You no believe me? Look am yourself!


In [ ]:
bob._objects

In [ ]:
z = x_ptr + x_ptr

In [ ]:
z

In [ ]:
bob._objects

Look this tin wella. You know sey when we call x.send(bob) he return a new object wey we call x_ptr. This one na our first pointer to a tensor. Pointers to tensors no dey hold data by themselves. Instead, they just contain metadata about a tensor (with data) wey we store on top another machine. The purpose of these tensors na to give us intuitive API wey go tell other machine to compute functions wey dey use this tensor. Make we look metadata wey pointers contain.


In [ ]:
x_ptr

Look that metadata wella!

He get two main attributes wey pointers get:

  • x_ptr.location : bob, the location, reference to the location wey the pointer dey point
  • x_ptr.id_at_location : <random integer>, the id where the tensor dey stored at location

Na this format dem dey print am <id_at_location>@<location>

He still get other generic attributes:

  • x_ptr.id : <random integer>, the id of our pointer tensor, it was allocated randomly
  • x_ptr.owner : "me", the worker wey be the owner of the pointer tensor, here na the local worker, make we say "me"

In [ ]:
x_ptr.location

In [ ]:
bob

In [ ]:
bob == x_ptr.location

In [ ]:
x_ptr.id_at_location

In [ ]:
x_ptr.owner

You fit dey reason why the local worker wey own the pointer na VirtualWorker, although we no create am. Make you laugh small, just as we get VirtualWorker object for Bob, we dey always have one for us. We fit create this worker automatically when we call hook = sy.TorchHook() so no disturb yourself about creating it yourself.


In [ ]:
me = sy.local_worker
me

In [ ]:
me == x_ptr.owner

As we done dey end, just as we call .send() on a tensor, we fit call .get() on a pointer to a tensor to get am back!!!


In [ ]:
x_ptr

In [ ]:
x_ptr.get()

In [ ]:
y_ptr

In [ ]:
y_ptr.get()

In [ ]:
z.get()

In [ ]:
bob._objects

As you don see sey Bob no get the tensors again!!! They don go back to our machine!

Section 1.2 - Make we use Tensor Pointers

To dey send and receive tensors from Bob dey great, but the tin dey hard too dey do in Deep Learning! We wan make sure sey we fit perform tensor operations on remote tensors. As luck come dey my side, tensors pointers dey make am dey easy. You fit use pointers as you dey use normal tensors!


In [ ]:
x = torch.tensor([1,2,3,4,5]).send(bob)
y = torch.tensor([1,1,1,1,1]).send(bob)

In [ ]:
z = x + y

In [ ]:
z

And voilà!

Powerful sometin dey happen behind the scenes. Instead make we compute x and y on locally, we go run command wey dey serialized and go come send am to Bob, who go perform the computation, tensor z go dey created, and then returned the pointer to z wey go come back to us!

If we call .get() on the pointer, we go receive the result back to our machine!


In [ ]:
z.get()

Torch Functions

We don extend this API to all Torch operations!!!


In [ ]:
x

In [ ]:
y

In [ ]:
z = torch.add(x,y)
z

In [ ]:
z.get()

Variables (including backpropagation!)


In [ ]:
x = torch.tensor([1,2,3,4,5.], requires_grad=True).send(bob)
y = torch.tensor([1,1,1,1,1.], requires_grad=True).send(bob)

In [ ]:
z = (x + y).sum()

In [ ]:
z.backward()

In [ ]:
x = x.get()

In [ ]:
x

In [ ]:
x.grad

As we don see sey API dey flexible and capable of performing any operation we fit perform in Torch on remote data. This one go prepare our mind for advance privacy preserving protocols as in Federated Learning, Secure Multi-Party Computation, and Differential Privacy !


In [ ]:

Congratulations!!! - Oya Join the Community!

Clap for una sef as you don finish this notebook tutorial! If you enjoy am and you wan join the movement towards privacy preserving, decentralized ownership of AI and the AI supply chain (data), follow the steps wey dey below.

Star PySyft on GitHub

The easiset way to helep our community na to star the GitHub repos! This go helep raise awareness of the tools we dey build.

Join our Slack!

To follow up bumper to bumper on how latest advancements, join our community! You can do so by filling out the form at http://slack.openmined.org

Join a Code Project!

The best way to contribute to our community na to become code contributor! You fit go to PySyft GitHub Issues page and filter for "Projects". E go show you all the top level Tickets giving an overview of what projects you fit join! If you no wan join any project, but you wan code small, you fit look for more "one off" mini-projects by searching for GitHub issues marked "good first issue"

If you no get time to contribute to our codebase, but still like to lend support, you fit be a Backer on our Open Collective. All donations wey we get na for our web hosting and other community expenses such as hackathons and meetups! meetups!

OpenMined's Open Collective Page


In [ ]: